package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import bean.SgrjhShSearchBean;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Created by Jianqi on 2017/9/12.
 *
 * 创建施工日计划审核的条件搜索View
 */

public class SgrjhShSearchView extends RelativeLayout {
    //类别
    TextView lb;
    //线名
    TextView xm;
//    //开始日期
//    TextView ksrq;
//    //结束日期
//    TextView jsrq;
    //计划号
    TextView jhh;
    //单位和部门
    EditText dwhbm;
    //项目名称
//    EditText xmmc;
    //状态
    TextView zt;
    TextView title_jhh;
    //状态 layout
    LinearLayout zt_layout;
    //确定
    TextView confirm;
    //取消
    TextView cancel;
    //清空
    TextView delect;

    ListPopWindow dwhbmPop;
    private List<UnitInformation> mAutoSource = new ArrayList<>();

    OnClick mOnClick;
    SgrjhShSearchBean mSearchBean;

    private ListPopWindow ztListPopWindow;
    private ListPopWindow lbListPopWindow;
    private List ztlist = new ArrayList();
    private List lblist = new ArrayList();

    public SgrjhShSearchView(Context context) {
        super(context);
        initView();
        initListener();
    }

    public SgrjhShSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initListener();
    }

    public SgrjhShSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initListener();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.dialog_sgrjhsh_search, this);
        lb = (TextView) findViewById(R.id.lb);
        xm = (TextView) findViewById(R.id.xm);
//        ksrq = (TextView) findViewById(R.id.ksrq);
//        jsrq = (TextView) findViewById(R.id.jsrq);
        jhh = (TextView) findViewById(R.id.jhh);
        dwhbm = (EditText) findViewById(R.id.dwhbm);
        zt = (TextView) findViewById(R.id.zt);
        title_jhh = (TextView) findViewById(R.id.title_jhh);
        zt_layout = (LinearLayout) findViewById(R.id.zt_layout);
        confirm = (TextView) findViewById(R.id.confirm);
        cancel = (TextView) findViewById(R.id.cancel);
        delect = (TextView) findViewById(R.id.delect);
        if (mSearchBean == null) {
            mSearchBean = new SgrjhShSearchBean();
        }
        if (dwhbmPop == null) {
            dwhbmPop = new ListPopWindow.Builder(getContext())
                    .setList(DictionariesUtil.getUnitList())
                    .setOutsideTouchable(false)
                    .setParentView(dwhbm)
                    .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                        @Override
                        public void OnListItemCilck(int position) {
                            dwhbm.setText(DictionariesUtil.getSgdwUnitList().get(position).toString());
                            dwhbmPop.dismiss();
                        }
                    }).builder();
        }
    }

    private void initListener() {

        //单位
        dwhbm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    return;
                } else {
                    if (!dwhbmPop.getWindow().isShowing()) {
                        dwhbmPop.show();
                    }
                    mAutoSource = MyApplication.getDaoSession()
                            .getUnitInformationDao()
                            .queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.like("%" + s + "%")).list();
                    if (mAutoSource.size() == 0) {
                        UnitInformation un = new UnitInformation();
                        un.setLjyyxDwxxName("没有匹配的单位");
                        mAutoSource.add(un);
                    }
                    dwhbmPop.setList(mAutoSource);
                }
            }
        });

        //类别
        lblist = Arrays.asList(getContext().getResources().getStringArray(R.array.lb));
        lb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lbListPopWindow == null) {
                    lbListPopWindow = new ListPopWindow.Builder(getContext()).setList(lblist)
                            .setParentView(lb)
                            .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                                @Override
                                public void OnListItemCilck(int position) {
                                    lb.setText(lblist.get(position).toString());
                                    lbListPopWindow.dismiss();
                                }
                            }).builder();
                }
                lbListPopWindow.show();
            }
        });
        //状态
        ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.sgrjhsh_zt));
        zt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ztListPopWindow == null) {
                    ztListPopWindow = new ListPopWindow.Builder(getContext()).setList(ztlist)
                            .setParentView(zt)
                            .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                                @Override
                                public void OnListItemCilck(int position) {
                                    zt.setText(ztlist.get(position).toString());
                                    ztListPopWindow.dismiss();
                                }
                            }).builder();
                }
                ztListPopWindow.show();
            }
        });
        //确定
        confirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchBean.lb = lb.getText().toString();
                mSearchBean.xm = xm.getText().toString();
//                mSearchBean.ksrq = ksrq.getText().toString();
//                mSearchBean.jsrq = jsrq.getText().toString();
                mSearchBean.jhh = jhh.getText().toString();
//                mSearchBean.xmmc = xmmc.getText().toString();
                mSearchBean.zt = zt.getText().toString();
                if (dwhbm.getText().length() != 0) {
                    List<UnitInformation> list = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(dwhbm.getText().toString())).list();
                    if (list.size() != 0) {
                        mSearchBean.setDwhbmid(list.get(0).getLjyyxDwxxId());
                    } else {
                        //如果没有对应的单位 则设置搜索ID为-1
                        mSearchBean.setDwhbmid("-1");
                    }
                }
                if (mOnClick != null) {
                    mOnClick.onConfirmListener(mSearchBean);
                }
                SgrjhShSearchView.this.setVisibility(GONE);
            }
        });
        //取消
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SgrjhShSearchView.this.setVisibility(GONE);
            }
        });
        //清空
        delect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                lb.setText("");
                xm.setText("");
//                ksrq.setText("");
//                jsrq.setText("");
                jhh.setText("");
                dwhbm.setText("");
//                xmmc.setText("");
                zt.setText("");
                mSearchBean = new SgrjhShSearchBean();
            }
        });
    }

    //对外暴露一个方法设置默认筛选时间
    public void setDefaultDate(SgrjhShSearchBean bean) {
        mSearchBean = bean;
//        if (mSearchBean.getKsrq() != null && mSearchBean.getJsrq() != null) {
//            ksrq.setText(mSearchBean.getKsrq());
//            jsrq.setText(mSearchBean.getJsrq());
//        }
    }



    public void setConfirmListener(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public interface OnClick {
        void onConfirmListener(SgrjhShSearchBean Util);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

}
